package xyz.xiezc.spider.downloader;

import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import xyz.xiezc.ioc.annotation.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wb-xzc291800
 * @date 2019/04/03 10:07
 */
@Component
public class CookieManager implements CookieJar {

    Map<String, List<Cookie>> cookieMap = new ConcurrentHashMap<>();

    @Override
    public void saveFromResponse(HttpUrl httpUrl, List<Cookie> list) {
        String host = httpUrl.host();
        cookieMap.put(host, list);
    }

    @Override
    public List<Cookie> loadForRequest(HttpUrl httpUrl) {
        String host = httpUrl.host();
        return cookieMap.get(host) == null ? new ArrayList<>() : cookieMap.get(host);
    }


}
